import json
from models import BaseModels
from sqlalchemy import Column, ForeignKey, Uuid, DateTime, BigInteger, Integer, Text, String


class Status(BaseModels):
    __tablename__ = 'status'
    id = Column(BigInteger, autoincrement=True, primary_key=True, unique=True, nullable=False)
    time = Column(DateTime, index=True, nullable=False, comment="触发时间")
    facility_id = Column(BigInteger, ForeignKey("facility.id", ondelete='CASCADE'), index=True, comment="报警设备ID")
    code = Column(Integer, nullable=False, comment="状态码")
    message = Column(String(64), index=True, comment="运行状态描述")
    account = Column(Text, nullable=True, comment="操作员")
    finish = Column(DateTime, nullable=True, comment="结束时间")
    expenditure = Column(Integer, nullable=True, comment="时间时长秒")
    session = Column(Uuid, nullable=False, index=True, comment="会话编号编号")

    def __repr__(self):
        return json.dumps({
            "andon_id": self.id,
            "time": self.time.strftime("%Y-%m-%d %H:%M:%S"),
            "facility_id": self.facility_id,
            "message": self.message,
            "account": self.account,
            "expenditure": self.expenditure
        })